//prepare proximity matrix. States defined as "proximal" if either adjacent or in same Census division.

***read in Kennan-Walker file
import delimited "$data/Adjacency/adjacency.csv", clear
gen id = _n
order id

merge 1:1 id using "$data/Crosswalks/stfips_id_crosswalk", keep(match) nogen
drop state_full state
ren statefips stfips
order id stfips

//rename variables
levelsof stfips, clean local(fips)
local counter = 0

foreach fip in `fips'{
	local counter `++counter'
	local current = word("`fips'", -`counter')
	local num = 51-`counter'
	ren v`num' v`current'
}

drop id
merge 1:1 stfips using "$data/Crosswalks/state_div_crosswalk", keep(match) nogen
drop state_full state
order stfips div

//loop over pairs of states
foreach fip1 in `fips'{

	foreach fip2 in `fips'{
		qui su division if stfips == `fip1'
		local div1 = `r(mean)'

		qui su division if stfips == `fip2'
		local div2 = `r(mean)'
		
		if `div1' == `div2'{
			replace v`fip2' = 1 if stfips == `fip1' //fill in if same division
		}
	}
}

//nothign proximal for alaska or hawaii
foreach fip in `fips'{
	replace v`fip' = 0 if stfips == 2
	replace v`fip' = 0 if stfips == 15
}


//export
drop division
save "$temp/state_prox_mat", replace
drop stfips
export delimited "$dir/Model/utilities/state_prox_mat.csv", replace novarn

//end of dofile

